<template>
  <f7-page @page:reinit="getDataItems">
    <!--<f7-page @pageReinit="getDataItems">-->
    <f7-navbar title="用章申请列表" back-link="">
      <f7-nav-right  >
        <f7-link icon-f7="add" href="/oa/workflow/applySealTable/-1"></f7-link>
      </f7-nav-right>
    </f7-navbar>

    <f7-list media-list>
      <f7-list-item
        swipeout
        v-for="(item) in items"
        v-bind:key="item.id"
        v-bind:title="item.orgName"
        v-bind:after="item.auditResult"
        v-bind:subtitle="sealNum[item.sealType]"
        v-bind:text="item.creationDate"
        @swipeout:open="saveCurItem(item)"
        @swipeout:deleted="delRecord">
        <f7-swipeout-actions right>
          <f7-swipeout-button close @click=" $refs.actionsOneGroup.open()">More</f7-swipeout-button>
        </f7-swipeout-actions>
      </f7-list-item>
    </f7-list>

    <f7-actions ref="actionsOneGroup">
      <f7-actions-group>
        <f7-actions-button @click="checkDetail">查看</f7-actions-button>
        <f7-actions-button v-show="isShow('编辑')" @click="editDetail">编辑</f7-actions-button>
        <f7-actions-button v-show="isShow('删除')" @click="delRecord">删除</f7-actions-button>
        <f7-actions-button v-show="isShow('启动')" @click="startApproval">启动</f7-actions-button>
        <f7-actions-button v-show="isShow('附件')" @click="startAttachManager">附件</f7-actions-button>

      </f7-actions-group>
      <f7-actions-group>
        <f7-actions-button color="red">Cancel</f7-actions-button>
      </f7-actions-group>
    </f7-actions>

  </f7-page>
</template>

<script>
  export default {
    name: "applySeal",
    data() {
      return {
        sealNum: this.GLOBAL.sealNum,
        items: [],
        curItem: null
      }
    },
    mounted() {
      this.$f7ready((f7) => {
        let that = this;
        f7.request.setup({
          headers: {
            'AccessToken': sessionStorage.getItem('accessToken'),
            'JSESSIONID': sessionStorage.getItem('userToken')
          }
        });
        f7.request.json(that.GLOBAL.RequestAddress + '/camel/rest/sealapplys', {
          'page': 1,
          'limit': 10,
          'jsonStr': '',
          'otherStr': '',
          'start': '0'
        }, function (result) {
          if (result.totalCount >=0 ) {
            that.items = result.data;
          }
        })
      })
    },
    watch: {
      items: {
        handler() {
        },
        deep: true
      }
    },
    methods: {
      getDataItems: function () {
        let that = this;
        let app = that.$f7;
        app.request.json(that.GLOBAL.RequestAddress + '/camel/rest/sealapplys', {
          'page': 1,
          'limit': 10,
          'jsonStr': '',
          'otherStr': '',
          'start': '0'
        }, function (result) {
          if (result.totalCount >=0) {
            that.items = result.data;
          }
        });
      },
      isShow: function (label) {
        let status = -1;
        let that = this;
        if (that.curItem == null) {
          return false;
        }
        status = that.curItem.status;
        switch (status) {
          case 0 :
            if (label.includes("编辑")) {
              return true;
            } else if (label.includes("启动")) {
              return true;
            } else if (label.includes("删除")) {
              return true;
            } else if (label.includes("附件")) {
              return true;
            }
            break;
          case 1:
            if (label.includes("编辑")) {
              return false;
            } else if (label.includes("启动")) {
              return false;
            } else if (label.includes("删除")) {
              return false;
            } else if (label.includes("附件")) {
              return false;
            }
            break;
          case 2:
            if (label.includes("编辑")) {
              return false;
            } else if (label.includes("启动")) {
              return false;
            } else if (label.includes("删除")) {
              return false;
            } else if (label.includes("附件")) {
              return false;
            }
            break;
        }
      },
      saveCurItem: function (item) {
        this.curItem = item;
      },
      checkDetail: function () {
        this.$f7router.navigate("/oa/applySealView/" + this.curItem.id)
      },
      editDetail: function () {
        this.$f7router.navigate("/oa/workflow/applySealTable/" + this.curItem.id)
      },
      delRecord: function () {
        let that = this;
        let url = that.GLOBAL.RequestAddress + "/camel/rest/sealapplys/" + that.curItem.id ;
        console.log(url)
        fetch(url,
          {
            method: "delete",
            headers: {
              "Content-Type": "application/json;charset=UTF-8",
              'AccessToken': sessionStorage.getItem('accessToken'),
              'JSESSIONID': sessionStorage.getItem('userToken')
            },
          }).then(function (response) {
          if (response.status == 200) {
            that.showAlert("提示", "记录删除成功")
            that.getDataItems();
          } else {
            that.showAlert("提示", "删除记录失败")
          }
        }).catch(function (err) {
          console.log("Fetch错误", err)
        });
      },
      startApproval: function () {
        let that = this;
        let url = that.GLOBAL.RequestAddress + "/camel/rest/sealapplys/workflow/startProcess";
        let app = that.$f7;
        app.dialog.confirm("流程启动后业务数据f将无法修改！确定要启动吗?", "提示", () => {
          app.request.json(url, {'id': that.curItem.id,}, function (result) {
            that.showAlert("提示", result["msg"])
            if (result.success == true) {
              that.getDataItems();
            }
          })
        })
      },
      showAlert: function (title, text) {
        let that = this;
        let app = that.$f7;
        app.dialog.alert(text, title, () => {
          //that.$f7router.back();
        });
      },
      uploadAddition: function(){
        this.$f7router.navigate("/oa/workflow/additionList/" + this.curItem.id)
      },
      startAttachManager : function () {
        WebViewJavascriptBridge.startAttachManager(this.curItem.id);
      }
    }
  }
</script>

<style scoped>

</style>
